﻿using C1.Win.C1Ribbon;
using CWCS.BLL;
using CWCS.BLL.NTIScModbus;
using CWCS.Common.Log;
using CWCS.Entity.SRM;
using CWCS.UIDesigner.SRMControl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CWCS.UI
{
    public partial class FrmSRMSend : C1RibbonForm
    {
        // SRM_Robot_Entity cbj ;
        private String SCType = "";
        private String OldLocationNum = "";
        private String BarCode = "";
        private String TaskNum = "";
        private String Num = "";
        ModbusBLLSRMRun taskbll = new ModbusBLLSRMRun();
        private ModbusCRCTask crc = new ModbusCRCTask();
        private bool isLineBlock;
        public static bool isChange = false;
        private LogHelper sclog = new LogHelper("SC");
        private LogHelper scManualLog = new LogHelper("scManualLog");

        public FrmSRMSend(object tag)
        {
            InitializeComponent();
            this.Num = tag.ToString();
            txtSCNum.Text = tag.ToString();
            txtSCNO.Text = tag.ToString();
            textBox1.Text = tag.ToString();
        }
        public FrmSRMSend(string SCNum, string TaskNum, string BarCode, string OldLocationNum, string Type, bool isLineBlock)
        {
            InitializeComponent();
            this.Num = SCNum;
            this.SCType = Type;
            this.BarCode = BarCode;
            this.TaskNum = TaskNum;
            this.OldLocationNum = OldLocationNum;
            this.isLineBlock = isLineBlock;

        }
        //public FrmSRMSend(SRM_Robot_Entity cbj, BLL.BLLConvery scbll, string Type)
        //{
        //    InitializeComponent();
        //    this.cbj = cbj;
        //    this.SCType = Type;

        //}

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void FrmSRMSend_Load(object sender, EventArgs e)
        {
            string[] status = crc.GetCRCStatus(this.Num.Trim());
            if (status != null)
            {
                txt_taskno.Text = status[3] == "0" ? "1000" : status[3];
                txt_taskno2.Text = status[16] == "0" ? "1000" : status[16];
                txt_from.Text = GetAdd("F", this.Num.Trim(), status[4]);// status[4] == "" ? "S0010010011" : status[4];
                txt_to.Text = GetAdd("T", this.Num, status[5]);// status[5] == "" ? "1078" : status[5];
            }
        }

        private string GetAdd(string type, string num, string add)
        {
            string address = "";
            if (type == "F")
            {
                switch (num)
                {
                    case "SRM001": address = string.IsNullOrEmpty(add) ? "S0010010011" : add; break;
                    case "SRM002": address = string.IsNullOrEmpty(add) ? "S0030010011" : add; break;
                    case "SRM003": address = string.IsNullOrEmpty(add) ? "S0050010011" : add; break;
                    case "SRM004": address = string.IsNullOrEmpty(add) ? "S0070010011" : add; break;
                    case "SRM005": address = string.IsNullOrEmpty(add) ? "S0090010011" : add; break;
                }
            }
            else
            {
                switch (num)
                {
                    case "SRM001": address = string.IsNullOrEmpty(add) ? "1019" : add; break;
                    case "SRM002": address = string.IsNullOrEmpty(add) ? "1028" : add; break;
                    case "SRM003": address = string.IsNullOrEmpty(add) ? "1037" : add; break;
                    case "SRM004": address = string.IsNullOrEmpty(add) ? "1041" : add; break;
                    case "SRM005": address = string.IsNullOrEmpty(add) ? "1055" : add; break;
                }
            }
            return address;
        }
        private void ChangeTaskType(string scno, SCTaskType taskType)
        {
            if (taskbll.UpdateTaskModeInfo(scno, taskType))
            {
                MessageBox.Show("修改任务模式成功");
            }
            else
            {
                MessageBox.Show("修改任务模式失败");
            }
        }
        private void ChangeForkNo(string scno, ForkNoType forkNo)
        {
            if (taskbll.UpdateForkNoInfo(scno, forkNo))
            {
                MessageBox.Show("修改货叉类型成功");
            }
            else
            {
                MessageBox.Show("修改货叉类型失败");
            }
        }
        private void manualButton_Click(object sender, MouseEventArgs e)
        {
            SRMCommand cmdType = SRMCommand.None;
            ForkCommand forkNo = ForkCommand.None;
            string strCommandType = cmbCommandType.Text.Trim();
            string forkInfo = comboBox1.Text.Trim();


            if (strCommandType == "--请选择--")
            {
                MessageBox.Show("请选择命令！");
                return;
            }
            else if (strCommandType == "移动")
            {
                cmdType = SRMCommand.CR_POS_GET;
            }
            else if (strCommandType == "取放货")
            {
                cmdType = SRMCommand.PutAndGet;
            }
            else if (strCommandType == "取货")
            {
                cmdType = SRMCommand.CR_GETC;
            }
            else if (strCommandType == "放货")
            {
                cmdType = SRMCommand.CR_PUTC;
            }
            else if (strCommandType == "清除任务")
            {
                cmdType = SRMCommand.CR_NO_FUNC;
            }
            if (forkInfo == "--请选择--")
            {
                MessageBox.Show("请选择要下发的货叉！");
                return;
            }
            else if (forkInfo == "货叉1")
            {
                forkNo = ForkCommand.ForkNo1;
            }
            else if (forkInfo == "货叉2")
            {
                forkNo = ForkCommand.ForkNo2;
            }
            else if (forkInfo == "货叉12")
            {
                forkNo = ForkCommand.ForkNoAll;
            }

            if (cmdType == SRMCommand.None)
                return;
            if (forkNo == ForkCommand.None)
                return;
            if (forkNo == ForkCommand.ForkNoAll)
            {
                if (txt_to.Text.Trim().Length > 4 && txt_from.Text.Trim().Length > 4)
                {
                    MessageBox.Show("货叉选择有误!货架移库请使用单货叉");
                    return;
                }
            }
            CRCObject si = BLLBase.list_srm_object.Find(
                  delegate (CRCObject cc)
                  {
                      return cc.ScNo == txtSCNum.Text.Trim();
                  }
              );

            if (si != null)
            {
                if (txt_from.Text.Trim().Length > 4)
                {
                    if (!taskbll.getLocation(txt_from.Text.Trim(), txtSCNum.Text.Trim()))
                    {
                        MessageBox.Show("输入的货位编号不存在!请重新输入!货位号:" + txt_from.Text.Trim() + ",堆垛机编号:" + txtSCNum.Text.Trim());
                        return;
                    }
                }
                if (txt_to.Text.Trim().Length > 4)
                {
                    if (!taskbll.getLocation(txt_to.Text.Trim(), txtSCNum.Text.Trim()))
                    {
                        MessageBox.Show("输入的货位编号不存在!请重新输入!货位号:" + txt_to.Text.Trim() + ",堆垛机编号:" + txtSCNum.Text.Trim());
                        return;
                    }
                }
                if (si.forkType == "2")
                {
                    if (txt_to.Text.Trim().Length == 4)
                    {
                        string workNo1tostation = BLLBase.converyZoneInfo.Where(x => x.stationNo == txt_to.Text.Trim()).FirstOrDefault().WORKINGLOCATION1;//工位1
                        string workNo2tostation = BLLBase.converyZoneInfo.Where(x => x.stationNo == txt_to.Text.Trim()).FirstOrDefault().WORKINGLOCATION2;//工位2
                        if (forkNo == ForkCommand.ForkNo1)
                        {
                            if (txt_to.Text.Trim() != workNo1tostation)
                            {
                                MessageBox.Show("货叉或目标地址有误!");
                                return;
                            }
                        }
                        if (forkNo == ForkCommand.ForkNo2)
                        {
                            if (txt_to.Text.Trim() != workNo2tostation)
                            {
                                MessageBox.Show("货叉或目标地址有误!");
                                return;
                            }
                        }
                    }
                    if (txt_from.Text.Trim().Length == 4)
                    {
                        string workNo1tostation = BLLBase.converyZoneInfo.Where(x => x.stationNo == txt_from.Text.Trim()).FirstOrDefault().WORKINGLOCATION1;//工位1
                        string workNo2tostation = BLLBase.converyZoneInfo.Where(x => x.stationNo == txt_from.Text.Trim()).FirstOrDefault().WORKINGLOCATION2;//工位2
                        if (forkNo == ForkCommand.ForkNo1)
                        {
                            if (txt_from.Text.Trim() != workNo1tostation)
                            {
                                MessageBox.Show("货叉或起始地址有误!");
                                return;
                            }
                        }
                        if (forkNo == ForkCommand.ForkNo2)
                        {
                            if (txt_from.Text.Trim() != workNo2tostation)
                            {
                                MessageBox.Show("货叉或起始地址有误!");
                                return;
                            }
                        }
                    }
                }
                bool flag = SendTask(txt_taskno.Text.Trim(), txt_taskno2.Text.Trim(), txtSCNum.Text.Trim(), txt_from.Text.Trim(), txt_to.Text.Trim(), txt_taskno2.Text.Trim(), cmdType, (int)forkNo);
                string log = string.Format("用户:" + BLLBase.user + "手动下发堆垛机{0}指令：{1}，货叉1任务{2}，货叉2任务{3}，起始地址{4}，目标地址{5},货叉{6}", txtSCNum.Text, cmdType, txt_taskno.Text, txt_taskno2.Text, txt_from.Text, txt_to.Text, forkNo);
                scManualLog.Info(log);
                FrmLogIn.username = "";
                if (flag)
                {
                    this.Close();
                }
            }
        }
        private bool SendTask(string taskno,string taskno2, string scno, string sourceAddress, string targetAddress, string BarCode, SRMCommand cmdType,int forkNo)
        {
            if (taskbll.SendTask(scno, taskno, taskno2, sourceAddress, targetAddress, cmdType, "0", forkNo))
            {
                MessageBox.Show("指令下发成功");
                return true;
            }
            else
            {
                MessageBox.Show("指令下发失败");
                return false;
            }
        }
        private void c1Button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void modeButton_Click(object sender, EventArgs e)
        {
            SCTaskType tasktype = SCTaskType.All;
            string strCommandType = cmbTaskMode.Text.Trim();
            if (strCommandType == "--请选择--")
            {
                MessageBox.Show("请选择命令！");
                return;
            }
            else if (strCommandType == "正常出入")
            {
                tasktype = SCTaskType.All;
            }
            else if (strCommandType == "暂停任务")
            {
                tasktype = SCTaskType.None;
            }
            else if (strCommandType == "仅入库")
            {
                tasktype = SCTaskType.Task_InBound;
            }
            else if (strCommandType == "仅出库")
            {
                tasktype = SCTaskType.Task_OutBound;
            }


            ChangeTaskType(txtSCNum.Text.Trim(), tasktype);
            string log = string.Format("用户:"+BLLBase.user+"更改堆垛机任务模式：堆垛机号{1}，更改后模式为{2}", FrmLogIn.username, txtSCNum.Text.Trim(), strCommandType);
            scManualLog.Info(log);
            this.Close();
        }
        private void forkNoButton_Click(object sender, EventArgs e)
        {
            ForkNoType forkNo = ForkNoType.forkNo;
            string strCommandType = cmbforkNo.Text.Trim();
            if (strCommandType == "--请选择--")
            {
                MessageBox.Show("请选择货叉！");
                return;
            }
            else if (strCommandType == "货叉1")
            {
                forkNo = ForkNoType.forkNo1;
            }
            else if (strCommandType == "货叉2")
            {
                forkNo = ForkNoType.forkNo2;
            }
            else if (strCommandType == "双货叉")
            {
                forkNo = ForkNoType.forkNo;
            }
            ChangeForkNo(txtSCNum.Text.Trim(), forkNo);
            string log = string.Format("用户:" + BLLBase.user + "更改堆垛机货叉类型：堆垛机号{1}，更改后货叉为{2}", FrmLogIn.username, txtSCNum.Text.Trim(), strCommandType);
            scManualLog.Info(log);
            this.Close();
        }
    }
}
